package com.renren.api.connect.android.demo;

import android.app.TabActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;

import com.renren.api.connect.android.R;
import com.renren.api.connect.android.demo.activity.ApiListActivity;
import com.renren.api.connect.android.demo.activity.StatusPublishActivity;

public class MainActivity extends TabActivity implements OnTabChangeListener {
	private TabHost mTabHost = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 设置隐藏标题
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 设置竖屏

		setContentView(R.layout.main_layout);

		mTabHost = getTabHost();

		// initialize tabs and tab intents
		addTab("MORE", R.string.tab_info, R.drawable.ic_tab_recommend,
				RenrenSDKDemo.class);
		
		addTab("HOME", R.string.tab_info, R.drawable.ic_tab_category,
				ApiListActivity.class);

		addTab("CATEGORY", R.string.tab_info, R.drawable.ic_tab_downloads,
				StatusPublishActivity.class);

		addTab("SEARCH", R.string.tab_info, R.drawable.ic_tab_rating,
				RenrenSDKDemo.class);

		addTab("MORE", R.string.tab_info, R.drawable.ic_tab_search,
				RenrenSDKDemo.class);

		// 设置每个Tab项的背景
		for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
			mTabHost.getTabWidget().getChildAt(i).setBackgroundDrawable(
					getResources().getDrawable(R.drawable.tab_selector_bg));
		}

		// set the default current tab
		// mTabHost.setCurrentTab(0);
		mTabHost.setPadding(0, 0, 0, -5);
		this.mTabHost.setOnTabChangedListener(this);
	}

	/**
	 * 设置Tab项，绑定Intent信息
	 * 
	 * @param tag
	 *            Tag
	 * @param title
	 *            名称
	 * @param icon
	 *            图标
	 * @param c
	 *            目标Activity
	 */
	private void addTab(String tag, int title, int icon, Class<?> c) {
		View item = LayoutInflater.from(this).inflate(
				R.layout.tab_main_indicator, null);
		TextView txtTitle = (TextView) item.findViewById(R.id.tab_title);
		ImageView img = (ImageView) item.findViewById(R.id.tab_icon);
		txtTitle.setText(title);
		img.setBackgroundResource(icon);
		TabHost.TabSpec spec = mTabHost.newTabSpec(tag).setIndicator(item)
				.setContent(new Intent().setClass(this, c));

		mTabHost.addTab(spec);
	}

	public void onTabChanged(String tabId) {

	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	@Override
	protected void onResume() {
		super.onResume();
	}
}